/**
 * @author v.lugovksy
 * created on 16.12.2015
 */
(function () {
    'use strict';

    angular.module('KingAdmin.pages.pdev')
        .directive('inputTree', inputTree);

    /** @ngInject */
    function inputTree() {
        return {
            restrict: 'EA',
            scope: {
                nodeName: '=',
                nodeId: '=',
                nodes: '=',
                title: '@'
            },
            template: '<input type="text" id="parent" ng-click="selectTree()" class="form-control" ng-model="nodeName" />',
            controller: function ($scope, $element, $attrs, $uibModal, $filter) {
                // 设置分组名称
                if ($scope.nodeId && $scope.nodes) {
                    var selected = $filter('filter')($scope.nodes, {id: $scope.nodeId});
                    console.log(selected);
                    $scope.nodeName = selected.length ? selected[0].text : '';
                }

                $scope.selectTree = function() {
                    var modalInstance = $uibModal.open({
                        size: 'sm',
                        animation: true,
                        templateUrl: 'app/pages/pdev/devgroup/devgroupmodal.html',
                        controller: 'DevgroupModalCtrl',
                        controllerAs: 'kt',
                        backdrop: 'static',
                        resolve: {
                            nodes: function () {
                                return $scope.nodes;
                            },
                            title: function () {
                                return $scope.title;
                            }
                        }
                    });

                    modalInstance.result.then(function (result) {
                        console.log(result);
                        $scope.nodeName = result.text;
                        $scope.nodeId = result.id;
                    }, function (reason) {
                        console.log(reason);
                    })
                }
            }
        };
    }
})();